﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace IReklamo
{
    public class ComplaintRepository
    {

        private class ComplaintDtO : Complaint { }
        private class SponsoredComplaintDtO : SponsoredComplaint { }

        public List<string> AddComplaint(Complaint complaint)
        {
            using (var db = new IReklamoDbContext())
            {
                db.Complaints.Add(complaint);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    //log exception
                    throw ex;
                }
            }
            return new List<string>();
        }

        public Complaint GetComplaint(int complaintId)
        {
            Complaint complaint = null;
            

            using (var db = new IReklamoDbContext())
            {
                db.Configuration.ProxyCreationEnabled = false;

                try
                {
                    var query = from c in db.Complaints.Include(c => c.Visuals).OfType<Complaint>()
                                where c.Id == complaintId
                                select new ComplaintDtO
                                {
                                    Comment = c.Comment,
                                    Complainee = c.Complainee,
                                    Id = c.Id,
                                    ShowIdentity = c.ShowIdentity,
                                    Title = c.Title,
                                    Visuals = c.Visuals,
                                    TotalSupports = c.Supports.Count(),
                                    TotalDislikes = c.Dislikes.Count(),
                                    TotalComments = c.Comments.Count()
                                };

                    var list = query.ToList();
                    var dto = query.FirstOrDefault();
                    complaint = (Complaint) query.FirstOrDefault();
                }
                catch (Exception ex)
                {
                    //log exception
                    throw ex;
                }
            }
            return complaint;
        }

        public List<string> AddVisualToComplaint(Visual visual)
        {
            using (var db = new IReklamoDbContext())
            {
                db.Visuals.Add(visual);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {

                }
            }
            return new List<string>();
        }

        public List<Complaint> GetLatestComplaints()
        {
            using (var db = new IReklamoDbContext())
            {
                db.Configuration.ProxyCreationEnabled = false;

                var query = from c in db.Complaints.Include(c => c.Visuals).OfType<Complaint>()
                            select new ComplaintDtO
                             {
                                 Comment = c.Comment,
                                 Complainee = c.Complainee,
                                 Id = c.Id,
                                 ShowIdentity = c.ShowIdentity,
                                 Title = c.Title,
                                 Visuals = c.Visuals,
                                 TotalSupports = c.Supports.Count(),
                                 TotalDislikes = c.Dislikes.Count(),
                                 TotalComments = c.Comments.Count()
                             };

                var list = query.ToList();
                return list.Select(c => (Complaint)c).ToList();
            }
        }

        public List<SponsoredComplaint> GetLatestSponsoredComplaints()
        {
            using (var db = new IReklamoDbContext())
            {
                db.Configuration.ProxyCreationEnabled = false;

                var query = from c in db.Complaints.Include(c => c.Visuals).OfType<SponsoredComplaint>()
                            select new SponsoredComplaintDtO
                             {
                                 Comment = c.Comment,
                                 Complainee = c.Complainee,
                                 Id = c.Id,
                                 ShowIdentity = c.ShowIdentity,
                                 Title = c.Title,
                                 Visuals = c.Visuals,
                                 TotalSupports = c.Supports.Count(),
                                 TotalDislikes = c.Dislikes.Count(),
                                 TotalComments = c.Comments.Count(),
                                 TeaserTitle = c.TeaserTitle
                             };

                var list = query.ToList();
                return list.Select(c => (SponsoredComplaint)c).ToList();
            }
        }


        public List<string> AddSupport(int complaintId)
        {
            using (var db = new IReklamoDbContext())
            {
                db.Supports.Add(new Support
                {
                    ComplaintId = complaintId
                });
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {

                }
            }
            return new List<string>();
        }

        public List<string> AddDislike(int complaintId)
        {
            using (var db = new IReklamoDbContext())
            {
               
            }
            return new List<string>();
        }

        public List<string> AddComment(Comment comment)
        {
            using (var db = new IReklamoDbContext())
            {
                db.Comments.Add(comment);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {

                }
            }
            return new List<string>();
        }

        public List<Comment> GetComments(int complaintId)
        {
            using (var db = new IReklamoDbContext())
            {
                return db.Comments.Where(c => c.ComplaintId == complaintId).ToList();
            }
        }
    }
}
